package org.jeecg.modules.purchase.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.config.mqtoken.UserTokenContext;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.api.model.TimingLog;
import org.jeecg.common.util.TimingUtils;
import org.jeecg.modules.purchase.entity.MerchantProvider;
import org.jeecg.modules.purchase.service.IMerchantProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class UpdateMerchant {
    
    @Autowired
    private IMerchantProviderService merchantProviderService;
    
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    
    private static final String ENABLE = "Y";
    
    private static final String DISABLE = "N";
    
    private static final String NDEL_FLAG = "0";
    
    private static final String FAILED_STATUS = "3";
    
    
    /**
     * 定时修改过期供应商状态
     * @param param
     * @return
     */
    @XxlJob("updateMerchant")
    public ReturnT<String> updateMerchant(String param) {
        String expiredDay = DateUtil.format(new Date(), "yyyy-MM-dd");
        //查询出所有过期时间为当天日期的数据，修改启用禁用状态以及签约状态
        List<MerchantProvider> merchantProviders = merchantProviderService.getByEndTime(expiredDay);
        if(ObjectUtil.isNotEmpty(merchantProviders)){
            for(MerchantProvider merchantProvider : merchantProviders){
                String id = merchantProvider.getId();
                boolean result =  merchantProviderService.expriedUpdate(id);
                if (result){
                    //添加日志
                    TimingLog log = new TimingLog();
                    log.setNoticeTitle(merchantProvider.getName()+ "到期禁用");
                    log.setNoticeId(merchantProvider.getId());
                    log.setModelName("招商");
                    log.setCreateBy("admin");
                    log.setCreateTime(new Date());
                    UserTokenContext.setToken(TimingUtils.getTemporaryToken());
                    sysBaseAPI.add(log);
                }
            }
        }
        return ReturnT.SUCCESS;
    }
}
